/*
 * LCD Driver for PE12864-004-H-P1 / ST7565S
 * Platform : PIC18F4550
 * Author: percy, 2007/09/21
 */

#define _XTAL_FREQ 12000000

//#include <p18cxxx.h>
//#include <delays.h>
#include <xc.h>
#include "lcd.h"

#define LCD_RS  LATCbits.LATC6
#define LCD_RW  LATCbits.LATC1
#define LCD_E   LATCbits.LATC2
#define LCD_RES LATCbits.LATC0
#define LCD_CS  LATCbits.LATC7
#define LCD_LED LATBbits.LATB5

#define ADC_REV             0xA1
#define COM_NORMAL          0xC0	
#define COM_REVERVE         0xC8	
#define BIAS_0              0xA2
#define POWER_CTL           0x28
#define REGULAR_RESISTOR    0x20
#define ELECTRONIC_VOL_MODE 0x81
#define ELECTRONIC_VOL_REG  0x00
#define DISPLAY_ALL_POINT		0xA5


#define LCD_ON       0xAF
#define LCD_OFF      0xAE

#define SET_LINE     0x40
#define SET_PAGE     0xB0
#define SET_COL_HIGH 0x10
#define SET_COL_LOW  0x04

void gLcdCheckBusy(void)
{
	unsigned char temp;

	LCD_RS=0;
	LCD_RW=1;
	TRISD = 0xFF;
	do{
		LCD_CS=0;
		LCD_E=1;
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		Nop();
		temp=PORTD;
		//LCD_E=0;
		//LCD_CS=1;
		__delay_ms(10);
	}
	while(temp&0x80);
	LCD_E=0;
	LCD_CS=1;
}		

void gLcdCommand(unsigned char cmd)
{
	//gLcdCheckBusy();
	TRISD = 0;
	LCD_RS = 0;
	LCD_RW = 0;
	LCD_E	 = 1;
	Nop();
	Nop();
	Nop();
	Nop();
	//LCD_CS = 0;
	PORTD=cmd;
	Nop();
	Nop();
	Nop();
	Nop();
	LCD_E	 = 0;	
	//LCD_CS = 1;
}

void gLcdWriteData(unsigned char wdata)
{
	//gLcdCheckBusy();
	TRISD = 0;
	LCD_RS = 1;
	LCD_RW = 0;
	LCD_E=1;
	//LCD_CS=0;
	Nop();
	Nop();
	Nop();
	Nop();
	PORTD=wdata;
	Nop();
	Nop();
	Nop();
	Nop();
	LCD_E=0;
	//LCD_CS=1;
}

unsigned char gLcdReadData(void)
{
	unsigned char temp;
	TRISD = 0xFF;
	LCD_RS = 1;
	LCD_RW = 1;
	LCD_E=1;

	//LCD_CS = 0;
	temp=PORTD;
	LCD_E=0;
	//LCD_CS=1;
	return temp;
}

unsigned char gLcdReadStatus(void)
{
	unsigned char temp;
	TRISD = 0xFF;
	LCD_RS = 0;
	LCD_RW = 1;
	LCD_E=1;
	//LCD_CS = 0;
	temp=PORTD;
	LCD_E=0;
	//LCD_CS=1;

	return temp;
}
/*
const rom unsigned char table[8][128]={
 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xF8,0x68,0xF8,0xF0,0xD0,0x68,0x68,0xD0,0x00,0x00,0x80,0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xF8,0x80,0x80,0x80,0x80,0x98,0xFC,0xF0,0x80,0x9E,0xAD,0xAD,0x9E,0x98,0xBD,0xA5,0xB5,0x97,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0x40,0x00,0x00,0xF8,0x68,0xF8,0xF0,0x90,0xF8,0x68,0x98,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
 {0x00,0x00,0x00,0xF8,0x68,0xF8,0xF0,0xF0,0xF8,0x68,0xF8,0xF0,0x00,0x00,0xF8,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x81,0x80,0xC0,0xC1,0xE1,0x60,0x60,0x37,0x3F,0x3C,0x00,0x00,0x01,0x0F,0x0F,0x0C,0x06,0x06,0x06,0x06,0x07,0x03,0x03,0x03,0x03,0x03,0x03,0x01,0x01,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x07,0x06,0x06,0x06,0x0E,0x0C,0x0E,0x0F,0x1F,0x18,0x18,0x30,0x31,0x31,0x61,0x60,0xE0,0xC1,0xC1,0x81,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xA0,0xE0,0x00,0x00,0xE0,0x60,0x00}, 
 {0x00,0x00,0x00,0x01,0x01,0x01,0x00,0x00,0x01,0x01,0x07,0x9E,0xF8,0xF0,0xE1,0x60,0x30,0x38,0x18,0x1C,0x0C,0x06,0x06,0x03,0x03,0x01,0x01,0x81,0x80,0xC0,0xC0,0xE0,0x60,0x60,0x30,0x30,0x38,0x18,0x18,0x1C,0x0C,0x0C,0x0C,0x06,0x06,0x06,0x06,0x07,0x03,0x03,0x03,0x03,0x03,0x03,0x01,0x01,0x01,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x01,0x01,0x01,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x07,0x06,0x06,0x06,0x06,0x0C,0x0C,0x0C,0x1C,0x18,0x18,0x38,0x30,0x30,0x60,0x60,0xE0,0xC0,0xC0,0x80,0x81,0x01,0x01,0x03,0x03,0x06,0x0E,0x0C,0x1C,0x18,0x38,0x70,0x60,0xE0,0xC0,0x80,0x80,0x00,0x00,0x03,0x07,0x05,0x87,0xC0,0xC0,0x07,0x00,0x00}, 
 {0x00,0x00,0x02,0x07,0x0E,0x3C,0x38,0x1C,0x0C,0x06,0x07,0x03,0x01,0x00,0x80,0x80,0xC0,0xE0,0x70,0x30,0x38,0x18,0x0C,0x0E,0x06,0x07,0x03,0x03,0x01,0x01,0x80,0x80,0xC0,0xC0,0xE0,0x60,0x70,0x30,0x30,0xF8,0xE0,0x00,0x00,0x1C,0xFC,0xFC,0x86,0x06,0x06,0x06,0x07,0x03,0x1F,0xFF,0xF3,0x03,0x03,0x03,0xFF,0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x07,0x06,0x06,0x06,0x06,0x8C,0xFC,0x7C,0x1C,0x18,0x18,0x38,0x30,0x30,0x60,0x60,0xE0,0xC0,0xC0,0x80,0x80,0x01,0x01,0x03,0x03,0x07,0x06,0x0E,0x0C,0x18,0x38,0x30,0x70,0xE0,0xC0,0x80,0x80,0x01,0x01,0x03,0x07,0x0E,0x1C,0x1E,0x0F,0x03,0x01,0x00,0x00,0x00,0x00}, 
 {0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0x70,0x38,0x1C,0x1C,0x0E,0x07,0x03,0x01,0x00,0x00,0x80,0xC0,0xE0,0x60,0x70,0x38,0x38,0x7C,0xEE,0xC6,0x07,0x03,0x03,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0F,0x0F,0x08,0x00,0x00,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x07,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x06,0x0E,0x1C,0x18,0x38,0x70,0x60,0xE0,0xC0,0x80,0x80,0x01,0x01,0x03,0x07,0x06,0x0C,0x18,0x38,0x70,0xE0,0x40,0x00,0x00,0x00,0x00,0x00,0x00}, 
 {0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x0F,0x1F,0x39,0x70,0x60,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x40,0x40,0x40,0x40,0xC0,0xC0,0x00,0x00,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,0xC0,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0xC0,0xC0,0x00,0x40,0x40,0x40,0x40,0xC0,0xC0,0x40,0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x38,0x1C,0x0E,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 
 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x0F,0x18,0x10,0x10,0x10,0x10,0x10,0xFF,0xFF,0x00,0x00,0xFF,0x01,0x03,0x1C,0xE0,0x80,0xC0,0x3C,0x07,0x01,0xFF,0xFF,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 
 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,0x03,0x03,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};
*/

const unsigned char table1[8][128]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x60,0x60,0x60,0x60,0x31,0x37,0x3F,0xFF,0xF0,0x10,0x10,0x00,0x00,0x00,0x10,0x18,0xF8,0x7F,0x1F,0x0F,0x08,0x0C,0x0C,0x0C,0x0C,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0xFE,0xFC,0xFC,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFC,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0xF0,0xF0,0x20,0x30,0x10,0x10,0x10,0x10,0x18,0x18,0x98,0xF8,0xF8,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF9,0x79,0x30,0x00,0x00,0x04,0x0C,0x08,0x19,0x30,0x60,0xC0,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x03,0x00,0x00,0x00,0x00,0x00,0x20,0xC0,0xC0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xE0,0xFF,0xCF,0xC1,0x40,0xC0,0xFE,0xFC,0x2C,0x20,0x20,0x00,0xFF,0xFF,0x08,0x88,0x8C,0x8C,0x0C,0x04,0x04,0x06,0x07,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x40,0x60,0x20,0x30,0x18,0x0C,0x0E,0x27,0x33,0x31,0x30,0xB0,0xF0,0xF0,0x18,0x08,0x08,0x08,0x0C,0xFC,0xFC,0xF8,0x39,0x03,0x07,0x0F,0x0F,0x1E,0x1E,0x1E,0x1C,0x0C,0x0C,0x0C,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x60,0x38,0x1C,0x0F,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x0F,0x1F,0x7E,0xFC,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x01,0x01,0x1F,0x17,0x30,0x60,0xE0,0xF8,0xBF,0x8F,0x00,0x00,0x00,0xC0,0xFF,0x1F,0x00,0x00,0xFF,0xFF,0x17,0x10,0x10,0x10,0x18,0x18,0x18,0x18,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0x70,0x38,0x1E,0x0F,0x03,0x40,0x80,0x80,0x80,0x80,0xF0,0xFF,0x7F,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x20,0xE0,0xE0,0xFC,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x10,0x18,0x0C,0x06,0x07,0x01,0x01,0x03,0x07,0x27,0x10,0x0E,0x07,0x01,0x00,0x00,0x00,0x0F,0x3F,0x70,0x60,0xE0,0xE0,0xC0,0xC0,0xE0,0xE0,0xE0,0xF0,0x7C,0x7F,0x38,0x10,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};

const unsigned char table2[8][128]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0xE0,0xF0,0x70,0x60,0x00,0x00,0x03,0x7F,0xFF,0xFF,0x00,0x00,0x10,0x30,0x70,0xE0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x30,0xE4,0xC4,0x04,0x06,0x02,0x1B,0xF3,0x61,0x01,0x00,0x80,0x60,0x78,0xBE,0x9C,0x8C,0x80,0x80,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xF0,0xFF,0x3F,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x82,0x82,0x82,0xC3,0xC1,0xCF,0xFF,0xFE,0x40,0x60,0x60,0x60,0x20,0x20,0x30,0x3F,0xFF,0xF0,0x10,0x18,0x18,0x18,0x18,0x99,0x98,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x7C,0x3F,0x0F,0x06,0x86,0xE6,0xE6,0x02,0x22,0xC2,0x82,0x0A,0x1B,0x39,0x31,0x01,0x01,0x01,0xC5,0x89,0x99,0x39,0x31,0x01,0x09,0x0F,0x07,0x07,0x07,0x06,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xFC,0x9F,0x81,0xC0,0xC0,0xFC,0xF8,0x78,0x70,0x20,0x80,0x70,0x3C,0x1F,0x0F,0x0D,0xCC,0xCC,0x84,0x84,0x06,0x06,0x06,0x63,0x3F,0x1F,0x1E,0x0E,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x01,0x81,0x81,0x81,0xC1,0xC1,0xE0,0x60,0xFF,0xFF,0x10,0x18,0x08,0x08,0x04,0x00,0x00,0x00,0x01,0x1F,0xFC,0xE0,0xC0,0xE0,0xF8,0x3F,0x1F,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,0x43,0xC0,0x80,0xC0,0xFC,0x7D,0x5B,0x43,0x46,0x66,0x66,0x26,0xA6,0xE6,0xE7,0xE3,0x43,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x07,0x07,0x03,0x3F,0x6F,0x41,0xC1,0x80,0xE0,0x7C,0x3F,0x07,0x00,0x00,0x01,0x01,0xE0,0xF0,0x70,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x20,0x60,0xE0,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x03,0x03,0x07,0x03,0x03,0x01,0x41,0xC0,0xC0,0xE0,0xFF,0xFF,0x00,0x00,0x00,0x40,0x40,0x20,0x30,0x18,0x18,0x0C,0x0E,0x07,0x0F,0x1F,0x38,0xF0,0xE0,0xC0,0x80,0x80,0xC0,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x10,0x18,0x0C,0x06,0x07,0x83,0x81,0xC3,0xE6,0x6C,0x7C,0x38,0x3C,0x7E,0xE7,0xE3,0xC1,0xC0,0xC0,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x20,0x20,0x30,0x18,0x18,0x0C,0x07,0x03,0x03,0x0F,0x1E,0x3C,0x18,0x00,0x04,0x03,0x01,0x20,0x40,0xC0,0xC0,0xC0,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x07,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x07,0x07,0x07,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x02,0x03,0x03,0x03,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x07,0x07,0x07,0x03,0x03,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};


const unsigned char fontTable[11][2][8]={
	{	
		{0xFC,0xFE,0x07,0x63,0x63,0x07,0xFE,0xFC},
		{0x03,0x07,0x0E,0x0C,0x0C,0x0E,0x07,0x03}
	},
	{
		{0x00,0x06,0x06,0xFF,0xFF,0x00,0x00,0x00},
		{0x00,0x00,0x00,0x0F,0x0F,0x00,0x00,0x00}
	},
	{
		{0x04,0x06,0x87,0xC3,0xE3,0x77,0x3E,0x1C},
		{0x0E,0x0F,0x0F,0x0D,0x0C,0x0C,0x0C,0x0C}	
	},
	{
		{0x04,0x06,0x07,0x63,0x63,0xF7,0xDE,0x8C},
		{0x02,0x06,0x0E,0x0C,0x0C,0x0E,0x07,0x03}
	},
	{
		{0xE0,0xFF,0xDF,0xC0,0xC0,0xFE,0xFE,0xC0},
		{0x00,0x00,0x00,0x00,0x00,0x0F,0x0F,0x00}
	},
	{
		{0x3F,0x3F,0x33,0x33,0x33,0x73,0xE3,0xC3},
		{0x02,0x06,0x0E,0x0C,0x0C,0x0E,0x07,0x03}
	},
	{
		{0xFC,0xFE,0xC7,0x63,0x63,0xE7,0xC6,0x84},
		{0x03,0x07,0x0E,0x0C,0x0C,0x0E,0x07,0x03}
	},
	{
		{0x00,0x03,0x03,0xE3,0xF3,0x1B,0x0F,0x07},
		{0x00,0x00,0x00,0x0F,0x0F,0x00,0x00,0x00}
	},
	{
		{0x8C,0xDE,0xF7,0x63,0x63,0xF7,0xDE,0x8C},
		{0x03,0x07,0x0E,0x0C,0x0C,0x0E,0x07,0x03}
	},
	{
		{0x1C,0x3E,0x77,0x63,0x63,0x37,0xFE,0xFC},
		{0x02,0x06,0x0E,0x0C,0x0C,0x0E,0x07,0x03}
	},
	{
		{0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x00},
		{0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x00}
	}
};

void showMessage(void){
	unsigned char i,j;

	for(i=0;i<8;i++){
		gLcdCommand(SET_PAGE+i);
		gLcdCommand(SET_COL_HIGH);
		gLcdCommand(SET_COL_LOW);
		
		for(j=0;j<128;j++)
			gLcdWriteData(table1[i][j]);
	}

	for(i=0;i<20;i++) __delay_ms(50);

	for(i=0;i<8;i++){
		gLcdCommand(SET_PAGE+i);
		gLcdCommand(SET_COL_HIGH);
		gLcdCommand(SET_COL_LOW);
		
		for(j=0;j<128;j++)
			gLcdWriteData(table2[i][j]);
	}
	
	for(i=0;i<20;i++) __delay_ms(50);

	for(i=0;i<8;i++){
		gLcdCommand(SET_PAGE+i);
		gLcdCommand(SET_COL_HIGH);
		gLcdCommand(SET_COL_LOW);
		
		for(j=0;j<128;j++)
			gLcdWriteData(0);
	}	
}

void showDigit(unsigned char index,unsigned char offset){
	unsigned char i,j;
	unsigned char hh,ll;
	
	hh=offset/16;ll=offset%16;
	for(i=0;i<2;i++){
		gLcdCommand(SET_PAGE+i);
		gLcdCommand(SET_COL_HIGH+hh);
		gLcdCommand(SET_COL_LOW+ll);
		for(j=0;j<8;j++)
			gLcdWriteData(fontTable[index][i][j]);
	}
}

void lcd_test1(void){

	//gLcdCommand(0xA4);
	gLcdCommand(SET_PAGE);
	gLcdCommand(SET_COL_HIGH);
	gLcdCommand(SET_COL_LOW);
	gLcdWriteData(0xFF);

	gLcdCommand(SET_PAGE+1);
	gLcdCommand(SET_COL_HIGH+1);
	gLcdCommand(SET_COL_LOW);
	gLcdWriteData(0xFF);

}

void LCD_ClearDisplay(void) {
	unsigned char Page;
	unsigned char Column;

	for (Page = 0; Page < 8; Page++) // Loop for 8 memory 'pages'
	{
		gLcdCommand(SET_COL_HIGH);
		gLcdCommand(SET_COL_LOW);
		gLcdCommand(SET_PAGE+Page);
		for (Column = 0; Column < 128; Column++) gLcdWriteData(0);
	}
}

void lcd_initialize(void)
{
	unsigned char i,j;
	TRISC = 0;

	LCD_RES=1;	

	__delay_ms(40);  	//delay 40ms
	LCD_RES=0;
	__delay_ms(3);
	LCD_RES=1;
	LCD_E=0;
	LCD_CS=0;

	gLcdCommand(0xE2);	//interal reset
	gLcdCommand(0xae);
	gLcdCommand(0xa2);	//drive voltage 1/9
	gLcdCommand(0xa1);	//normal RAM display, ADC= 0
	gLcdCommand(0xc0);	//COM output scan in normal direction
	gLcdCommand(0x2f);	//internal power supply operating mode
	gLcdCommand(0x81);	//use volumn control function
	gLcdCommand(0x20);
	gLcdCommand(0x27);
	gLcdCommand(0x40);
	gLcdCommand(0xee);
	gLcdCommand(0xa6);
	gLcdCommand(0xa4);
	gLcdCommand(0x20);
	gLcdCommand(0xac);
	gLcdCommand(0xaf);
	
	gLcdCommand(SET_LINE); //?

}

